﻿using Common;
using CommonModel;
using IBll;
using MainService.Controllers.Extend;
using ResponseModel;
using System;
using System.Web.Http;

namespace MainService.Controllers
{
    /// <summary>
    /// 登录控制器
    /// </summary>
    public class LoginController : BaseApiController
    {
        public IAuthorizationBll AuthorizationBll { get; set; }
        public IUserInfoBll UserInfoBll { get; set; }

        /// <summary>
        /// 是否已经登录
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResultModel IsAlreadyLogin()
        {
            ResultModel resultModel = new ResultModel();

            try
            {
                if (AuthorizationBll.LoginAuthorization())
                {
                    resultModel.Pass = true;
                    resultModel.Msg = "用户已登录";
                }
                else
                {
                    resultModel.Msg = "用户未登录";
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("报错：" + ex);
                resultModel.Msg = "报错：" + ex;
            }

            return resultModel;
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="userInfoRequest"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultModel Login(UserInfoReqest userInfoRequest)
        {
            ResultModel resultModel = new ResultModel();

            try
            {
                //var obj = RequestBase["loginName"];//参数的类型需设置为：application/x-www-form-urlencoded
                LogHelper.Debug("入参：" + userInfoRequest.ToJson());
                string loginName = userInfoRequest.LoginName;
                string loginPassword = userInfoRequest.LoginPassword;
                if (UserInfoBll.Login(loginName, loginPassword))
                {
                    resultModel.Pass = true;
                    resultModel.Msg = "登录成功";
                }
                else
                {
                    resultModel.Msg = "登录失败";
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("报错：" + ex);
                resultModel.Msg = "报错：" + ex;
            }

            return resultModel;
        }

        /// <summary>
        /// 登出
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResultModel Logout()
        {
            ResultModel resultModel = new ResultModel();

            try
            {
                if (UserInfoBll.Logout())
                {
                    resultModel.Pass = true;
                    resultModel.Msg = "登出成功";
                }
                else
                {
                    resultModel.Msg = "登出失败";
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("报错：" + ex);
                resultModel.Msg = "报错：" + ex;
            }

            return resultModel;
        }
    }
}
